<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/11/9
 * Time: 14:15
 */

namespace Order\Controller;
use Common\Controller\AdminbaseController;
use Common\Log\TypeLog;
use Order\Model\OrderModel;
use Wx\Controller\WxPayController;

class AdminorderController extends AdminbaseController{

    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size = 10;
    }

    // 订单列表
    public function lists()
    {
        $index = I('index');
        $this->assign('index', $index);

        // 订单付款状态
        $status = I('post.status');
        if ($status) {
            $this->assign('status', $status);
            $where['o.status'] = $status;
        } else {
            $where['o.status'] = ['in', [OrderModel::ORDER_STATUS_NOT_PAY, OrderModel::ORDER_STATUS_HAS_PAY]];
        }

        // 退款状态
        $refund_status = I('refund_status');
        if ($refund_status) {
            $this->assign('refund_status', $refund_status);
            $where['o.refund_status'] = $refund_status;
        }

        // 时间限制
        $start_time = I('post.start_time');
        if (!empty($start_time)) {
            $where['o.created_at'][] = array('EGT', strtotime($start_time));
            $this->assign('start_time', $start_time);
        }
        $end_time = I('post.end_time');
        if (!empty($end_time)) {
            $where['o.created_at'][] = array('ELT', strtotime($end_time));
            $this->assign('end_time', $end_time);
        }

        $model  = M('order');
        $count  = $model->alias('o')->where($where)->count();
        $page   = $this->page($count, $this->page_size);
        $lists  = $model
            ->alias('o')
            ->join('__USERS__ u on u.id = o.user_id')
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order("created_at desc")
            ->field('o.*, u.user_nicename, u.avatar')
            ->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("lists", $lists);

        // 订单状态
        $order_status = array(
            OrderModel::ORDER_STATUS_NOT_PAY => '未支付',
            OrderModel::ORDER_STATUS_HAS_PAY => '已支付',
        );
        $this->assign('order_status', $order_status);

        // 退款状态
        $refund_lists = array(
            OrderModel::ORDER_REFUND_STATUS_NORMAL  => '正常',
            OrderModel::ORDER_REFUND_STATUS_ING     => '申请中',
            OrderModel::ORDER_REFUND_STATUS_HAS     => '已退款',
            OrderModel::ORDER_REFUND_STATUS_FAILED  => '退款失败',
        );
        $this->assign('refund_lists', $refund_lists);

        $this->display();
    }

    // 确认退款
    public function do_refund()
    {
        $order_id   = I('id');
        $admin_id   = sp_get_current_admin_id();
        $order_info = M('order')->where(['id' => $order_id, 'status' => OrderModel::ORDER_STATUS_HAS_PAY, 'exchange' => 0, 'refund_status' => OrderModel::ORDER_REFUND_STATUS_ING])->find();
        if ($order_id && $order_info) {

            // 拒绝退款申请
            $refuse = I('refuse');
            if ($refuse) {
                M('order')->where(['id' => $order_id])->save(['refund_status' => OrderModel::ORDER_REFUND_STATUS_FAILED]);
                TypeLog::addNormalLog($admin_id, $order_info['user_id'], TypeLog::LOG_TYPE_ORDER_REFUND_REFUSE, $order_id);
                $this->success('操作成功');
            } else {
                // 确认退款
                $wx = new WxPayController();
                $user_info = M('users')->where(['id' => $order_info['user_id']])->find();
                if (empty($user_info['oa_openid'])) {
                    $this->error('用户数据有误，请找最帅的技术解决');
                } else {
                    $result = $wx->refundOrder($order_info['trade_order_id'], $order_info['payment_money'], '活动订单退款', $user_info['oa_openid']);
                    if ($result['status'] < 0) {
                        $this->error($result['err_code_des']);
                    } else {
                        M('order')->where(['id' => $order_id])->save(['refund_status' => OrderModel::ORDER_REFUND_STATUS_HAS]);
                        TypeLog::addNormalLog($admin_id, $order_info['user_id'], TypeLog::LOG_TYPE_ORDER_REFUND, $order_id);
                        $this->success('退款成功');
                    }
                }
            }
        }
    }
}